package com.estate.struts.actions;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import com.estate.business.DeleProjectService;
import com.estate.utils.AnnouncementUtil;
import com.estate.utils.DeleProjectUtil;
import com.estate.utils.ProjectConstants;
import com.estate.vo.Vo;
import com.estate.vo.impl.AnnouncementVo;
import com.estate.vo.impl.DeleProjectVo;

public class DeleProjectAction extends EstateSupport {

	private String id;
	 private String proNum;
	 private String proName;
	 private String delePeople;
	 private String delePhone;
	 private String consCompany;
	 private String consPeople;
	 private String consPhone;
	 private String plan;
	 private String contractId;
	 private String proCatagory;
	 private String proCity;
	 private String proPeople;
	 private String proPhone;
	 private String remark;
	 private String deleDate;
	 private String recordDate;
	 private String postDate;
	 private String state;
	 private String tenderNum;
	
	private String method;
	
	private String multiType;
	
	private String multiDelete;
	private String delStr;
	
	private DeleProjectService deleProjectService;
	
	private String directPage;
	
	@Override
	public String execute() throws Exception {
		System.out.println("method ================= " + this.getMethod());
		System.out.println(this.getId());
		String page = "";
		if("add".equals(this.getMethod())){
			DeleProjectVo deleProject = this.getAddDeleProject();
			if(deleProject != null){
				boolean result = this.getDeleProjectService().addDeleProject(deleProject);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}else{
				page="fail";
			}
		}else if("list".equals(this.getMethod())){
			List<DeleProjectVo> projectList = this.getDeleProjectService().getMyDeleProjectList(this.getSessionUser());
			this.getRequest().setAttribute("projectList", projectList);
			if(this.getMultiType() != null && this.getMultiType().equals("true")){
				this.getRequest().setAttribute("multiType", new Boolean(true));
			}else{
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			page="allProject";
		}else if("detail".equals(this.getMethod())){
			Vo vo = this.getDeleProjectService().getDeleProject(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("project", vo);
			page = "detail";
		}else if("modify".equals(this.getMethod())){
			Vo vo = this.getDeleProjectService().getDeleProject(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("project", vo);
			page = "update";
		}else if("update".equals(this.getMethod())){
			DeleProjectVo project = this.getUpdateDeleProject();
			if(project == null){
				page = "fail";
			}else{
				boolean result = this.getDeleProjectService().updateDeleProject(project);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}
		}else if("post".equals(this.getMethod())){
			this.getDeleProjectService().updatePost(Integer.parseInt(this.getId()));
			page = this.SUCCESS;
		}else if("delete".equals(this.getMethod())){
			if("true".equals(this.multiDelete)){
				boolean result = this.getDeleProjectService().deleteMultiDeleProject(this.getDelStr());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
				
			}else{
//				System.out.println("single delete");
				if(this.getId() == null || "".equals(this.getId())){
					page="faild";
				}else{
					DeleProjectVo deleProject = new DeleProjectVo();
					deleProject.setId(Integer.parseInt(this.getId()));
					boolean result = this.getDeleProjectService().deleteDeleProject(deleProject);
					if(result){
						page=this.SUCCESS;
					}else{
						page="fail";
					}
				}
			}
		}else{
			page="fail";
		}
		if(this.getDirectPage() != null && !"".equals(this.getDirectPage())){
			page = this.getDirectPage();
		}
		return page;
	}
	private DeleProjectVo getUpdateDeleProject(){
		DeleProjectVo deleProject = new DeleProjectVo();
		
		int id = Integer.parseInt(this.getId());
		
		try {
			deleProject.setId(Integer.parseInt(this.getId()));
			deleProject.setConsCompany(this.getConsCompany());
			deleProject.setConsPeople(this.getConsPeople());
			deleProject.setConsPhone(this.getConsPhone());
			deleProject.setDeleDate(ProjectConstants.SIMPLE_DATE.parse(this.getDeleDate()));
			deleProject.setDelePeople(this.getDelePeople());
			deleProject.setDelePhone(this.getDelePhone());
			deleProject.setPlan(this.getPlan());
			deleProject.setProCatagory(this.getProCatagory());
			deleProject.setProCity(this.getProCity());
			deleProject.setProName(this.getProName());
			deleProject.setProNum(this.getProNum());
			deleProject.setProPeople(this.getProPeople());
			deleProject.setProPhone(this.getProPhone());
			deleProject.setRecordDate(ProjectConstants.SIMPLE_DATE.parse(this.getRecordDate()));
			deleProject.setRemark(this.getRemark());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		int stateInt = Integer.parseInt(this.getState());
		deleProject.setState(DeleProjectUtil.getDeleProjectString(stateInt));
		if(stateInt == 2){
			deleProject.setPostDate(new Date());
		}
		
		return deleProject;
	}
	private DeleProjectVo getAddDeleProject(){
		DeleProjectVo deleProject = new DeleProjectVo();
		
		//合同没有填写
		try {
			deleProject.setConsCompany(this.getConsCompany());
			deleProject.setConsPeople(this.getConsPeople());
			deleProject.setConsPhone(this.getConsPhone());
			deleProject.setDeleDate(ProjectConstants.SIMPLE_DATE.parse(this.getDeleDate()));
			deleProject.setDelePeople(this.getDelePeople());
			deleProject.setDelePhone(this.getDelePhone());
			deleProject.setPlan(this.getPlan());
			deleProject.setProCatagory(this.getProCatagory());
			deleProject.setProCity(this.getProCity());
			deleProject.setProName(this.getProName());
			deleProject.setProNum(this.getProNum());
			deleProject.setProPeople(this.getProPeople());
			deleProject.setProPhone(this.getProPhone());
			deleProject.setRecordDate(ProjectConstants.SIMPLE_DATE.parse(this.getRecordDate()));
			deleProject.setRemark(this.getRemark());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		int stateInt = Integer.parseInt(this.getState());
		deleProject.setState(DeleProjectUtil.getDeleProjectString(stateInt));
		if(stateInt == 2){
			deleProject.setPostDate(new Date());
		}
		deleProject.setPostUser(this.getSessionUser());
			
		return deleProject;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getMultiType() {
		return multiType;
	}
	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}
	public String getMultiDelete() {
		return multiDelete;
	}
	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}
	public String getDelStr() {
		return delStr;
	}
	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}
	public DeleProjectService getDeleProjectService() {
		return deleProjectService;
	}
	public void setDeleProjectService(DeleProjectService deleProjectService) {
		this.deleProjectService = deleProjectService;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getProNum() {
		return proNum;
	}
	public void setProNum(String proNum) {
		this.proNum = proNum;
	}
	public String getProName() {
		return proName;
	}
	public void setProName(String proName) {
		this.proName = proName;
	}
	public String getDelePeople() {
		return delePeople;
	}
	public void setDelePeople(String delePeople) {
		this.delePeople = delePeople;
	}
	public String getDelePhone() {
		return delePhone;
	}
	public void setDelePhone(String delePhone) {
		this.delePhone = delePhone;
	}
	public String getConsCompany() {
		return consCompany;
	}
	public void setConsCompany(String consCompany) {
		this.consCompany = consCompany;
	}
	public String getConsPeople() {
		return consPeople;
	}
	public void setConsPeople(String consPeople) {
		this.consPeople = consPeople;
	}
	public String getConsPhone() {
		return consPhone;
	}
	public void setConsPhone(String consPhone) {
		this.consPhone = consPhone;
	}
	public String getPlan() {
		return plan;
	}
	public void setPlan(String plan) {
		this.plan = plan;
	}
	public String getContractId() {
		return contractId;
	}
	public void setContractId(String contractId) {
		this.contractId = contractId;
	}
	public String getProCatagory() {
		return proCatagory;
	}
	public void setProCatagory(String proCatagory) {
		this.proCatagory = proCatagory;
	}
	public String getProCity() {
		return proCity;
	}
	public void setProCity(String proCity) {
		this.proCity = proCity;
	}
	public String getProPeople() {
		return proPeople;
	}
	public void setProPeople(String proPeople) {
		this.proPeople = proPeople;
	}
	public String getProPhone() {
		return proPhone;
	}
	public void setProPhone(String proPhone) {
		this.proPhone = proPhone;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public String getDeleDate() {
		return deleDate;
	}
	public void setDeleDate(String deleDate) {
		this.deleDate = deleDate;
	}
	public String getRecordDate() {
		return recordDate;
	}
	public void setRecordDate(String recordDate) {
		this.recordDate = recordDate;
	}
	public String getPostDate() {
		return postDate;
	}
	public void setPostDate(String postDate) {
		this.postDate = postDate;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getDirectPage() {
		return directPage;
	}
	public void setDirectPage(String directPage) {
		this.directPage = directPage;
	}
	public String getTenderNum() {
		return tenderNum;
	}
	public void setTenderNum(String tenderNum) {
		this.tenderNum = tenderNum;
	}
}
